import axios from 'axios'
import baseurl from '../http-config/config.js'
import requestMethod from '../http-config/request-method.js'

const nbaRequest = axios.create({
  baseURL: baseurl.nba,
  timeout: 1000 * 6,
})

//http request 拦截器
nbaRequest.interceptors.request.use(
  config => {
    // const token = getCookie('名称');注意使用的时候需要引入cookie方法，推荐js-cookie
    // config.data = JSON.stringify(config.data);
    config.headers = {
      'Content-Type': 'application/x-www-form-urlencoded',
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

//http response 拦截器
nbaRequest.interceptors.response.use(
  response => {
    // if(response.data.errCode ==2){
    //   router.push({
    //     path:"/login",
    //     querry:{redirect:router.currentRoute.fullPath}//从哪个页面跳转
    //   })
    // }
    return response
  },
  error => {
    if (error.message.includes('timeout')) {
      // 判断请求异常信息中是否含有超时timeout字符串
      Message.error({
        message: '请求超时！',
        duration: 3000,
      })
      return Promise.reject(error) // reject这个错误信息
    }
    return Promise.reject(error)
  }
)

const nbarequest = new requestMethod(nbaRequest)
export default nbarequest
